import time
import requests
import json

counter = 0
page = 0
while page <= 305:
    page += 1
    url = "https://api.bilibili.com/x/web-interface/newlist_rank"
    params = {
        'main_ver': 'v3',
        'search_type': 'video',
        'view_type': 'hot_rank',
        'copy_right': '-1',
        'new_web_tag': '1',
        'order': 'click',
        'cate_id': '247',
        'page': page,
        'pagesize': '30',
        'time_from': '20241016',
        'time_to': '20241023'
    }
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
    }
    response = requests.get(url, params=params, headers=headers)
    result = response.content.decode()
    result = json.loads(result)
    if not result['data']['result']:  # 如果result['data']['result']为空，则跳过本次循环
        time.sleep(2)
        print("没有返回数据...")
        page -= 1
        continue
    for item in result['data']['result']:
        title = item['title']
        play = item['play']
        counter += 1
        print(counter, title, play)
